package com.dc.project_kotlin.base

import android.widget.TextView
import androidx.viewbinding.ViewBinding
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.dc.base.ui.activity.BaseVmVbActivity
import com.dc.base.vm.BaseViewModel
import com.dc.project_kotlin.R

abstract class BaseActivity<VM: BaseViewModel, VB: ViewBinding> : BaseVmVbActivity<VM, VB>() {

    private var loadingDialog: MaterialDialog? = null

    override fun showLoading(message: String) {
        mActivity.let {
            if (!it.isFinishing) {
                if (loadingDialog == null) {
                    loadingDialog = MaterialDialog(it)
                        .cancelable(true)
                        .cancelOnTouchOutside(false)
                        .customView(R.layout.layout_custom_progress_dialog_view)
                        .lifecycleOwner(this)
                    loadingDialog?.getCustomView()?.run {
                        this.findViewById<TextView>(R.id.loading_tips).text = message
                    }
                }
                loadingDialog?.show()
            }
        }
    }

    override fun dismissLoading() {
        loadingDialog?.dismiss()
        loadingDialog = null
    }
}